package com.yiren.dbaa.util;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;

/**
 * Describe  GridViewUtil
 */
public class GridViewUtil {

    /**
     * 动态设置gridview高度
     */
    public static void setGridViewHeightByChildren(Context context, GridView gridView, int height) {
        //获取gridview高度
        ListAdapter listAdaper = gridView.getAdapter();
        if (listAdaper == null) {
            return;
        }
        //总高度
        int totalHeight = 0;
        //计算行数 向上取整
        int lineNum = (int) Math.ceil((double) listAdaper.getCount() / (double) gridView.getNumColumns());
        View item = listAdaper.getView(0, null, gridView);
        item.measure(0, 0);
        //获取高度和
        totalHeight = item.getMeasuredHeight() * lineNum;
        totalHeight = totalHeight > DensityUtil.dip2px(context, height)? DensityUtil.dip2px(context, height): totalHeight;
        //布局参数
        ViewGroup.LayoutParams params = gridView.getLayoutParams();
        //设置布局高度
        params.height = totalHeight;
        //设置margin
//        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        //设置参数
        gridView.setLayoutParams(params);
    }
}
